<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="二维码宽度">
          <a-input placeholder="请输入二维码宽度" v-decorator="['width', validatorRules.width]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="二维码高度">
          <a-input placeholder="请输入二维码高度" v-decorator="['height', validatorRules.height]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, downFile } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "ParkVehicleQrModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          width:{rules: [{ required: true, message: '请输入二维码宽度!' }]},
          height:{rules: [{ required: true, message: '请输入二维码高度!' }]},
        },
        url: {
          add: "/icbc/parkVehicleDetail/add",
          edit: "/icbc/parkVehicleDetail/qrcode",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'parkCode','parkName','armName','armCode','armStatus','createUser'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';   
            if(!this.model.id){
              httpurl+=this.url.add;
               method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'post';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
    //  let param = this.formData ;
              
      downFile(this.url.edit, formData).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }                       

        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]),   'qrcode.png')
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', this.model.parkName+ this.model.armName + "-" +this.model.armCode+".png")
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>